<?php

namespace Admin\Controller;

/**
 * 文化荟萃
 *
 * Class EventController
 * @package Admin\Controller
 */
class WorksController extends PublicController
{
    private $cultural_model;
    private $image_model;

    public function _initialize()
    {
        $this->cultural_model = M('cultural_blend');
        $this->image_model = M('blend_image');
        $this->assign('modules', '文化荟萃');
    }

    /**
     * 文化荟萃列表
     */
    public function index()
    {
        $where = "is_del=0";
        if (isset($_GET['keyword'])) {
            $where .= " AND title like '%" . trim($_GET['keyword']) . "%'";
            $this->assign('keyword', trim($_GET['keyword']));
        }
        $count = $this->cultural_model->where($where)->count();
        $perPage = 20;
        $page = isset($_GET['page']) ? (int)$_GET['page'] : 0;
        $totalPage = ceil($count / $perPage);
        $pageObj = $this->page_index($count, $totalPage, $page);
        $cul_list = $this->cultural_model->where($where)->order("sort asc")->limit($page * $perPage, $perPage)->select();
        $this->assign("cul_list",$cul_list);
        $this->assign('pageObj', $pageObj);
        $this->display();
    }

    /**
     * 文化日历编辑页面
     */
    public function add()
    {
        $this->assign('title', '添加活动');

        if (isset($_GET['id'])) {
            $detail = $this->cultural_model->where(['id' => intval($_GET['id']), 'is_del' => 0])->find();
            if (!empty($detail)) {
                $image = $this->image_model->where(['c_id'=>$_GET['id']])->select();
                $this->assign("image",$image);
                $this->assign('title', '修改活动');
                $this->assign('id', $_GET['id']);
                $this->assign("detail", $detail);
            }
        }else {
            $this->assign("id","0");
        }
        $this->display();
    }

    /**
     * 保存文化日历内容
     */
    public function save()
    {
        $title = I('post.title_1');
        $author = I('post.author_1');
        $c_sort = I('post.sort_1');
        $text = I('post.text/a');
        $sort = I('post.sort/a');
        $img =  I('post.img/a');
        $data = array(
            'title'=>$title,
            'author' => $author,
            'review' =>1,
            'sort' =>$c_sort,
        );
        if ( !empty(I('post.id'))) {

            $id = intval(I('post.id'));
             $this->cultural_model->where('id=' . $id)->save($data);
            $result_id = $id;
        } else {
            $data['create_at'] = date('Y-m-d H:i:s');
            $result_id = $this->cultural_model->add($data);
        }
        if ($result_id) {
            if (!empty($img)) {
                $imgs = array();
                foreach ( $img as $key=>$value ) {
                    $ff = $this->_get_image_name($value,$key);
                    $ff = str_replace("./Data","",$ff);
                    array_push($imgs,$ff);
                }
                $time = date("Y-m-d H:i:s");
                foreach ( $imgs as  $k=>$v ) {
                    $sql = " INSERT INTO lr_blend_image(c_id,intro,cover,sort,create_at) VALUES ($result_id,'".$text[$k]."','".$imgs[$k]."','".$sort[$k]."','$time')";
                    $this->cultural_model->execute($sql);
                }
            }

            $json_arr = array('done'=>true,'msg'=>'操作成功！');
            $json_obj = json_encode($json_arr);

        } else {
            $json_arr = array('done'=>false,'msg'=>'操作失败！');
            $json_obj = json_encode($json_arr);
        }
        echo $json_obj;
    }

    /**
     * 删除文化荟萃
     */
    public function del()
    {
        $id = intval($_REQUEST['did']);
        $info = $this->cultural_model->where('id=' . $id)->find();
        if (!$info) {
            $this->error('信息错误.');
            exit();
        }

        if (intval($info['del']) == 1) {
            $this->success('操作成功！.');
            exit();
        }

        $result = $this->cultural_model->where('id=' . $id)->save(['is_del' => 1]);
        if ($result) {
            $this->redirect('index');
        } else {
            $this->error('操作失败.');
        }
    }

    /**
     * 删除图集
     */
    public function del_img()
    {
        $id = I("post.id");
        $this->image_model->delete($id);
    }

    /**
     * 预约报名列表
     */
    public function apply()
    {
        $this->assign('title', '预约报名管理');
        $where = "1=1";
        if (isset($_GET['event_id']) && $eventId = intval($_GET['event_id'])) {
            $where .= " AND event_id = {$eventId}";
            $this->assign('event_id', $eventId);
        }
        $count = M('event_apply')->count();
        $perPage = 20;
        $page = isset($_GET['page']) ? (int)$_GET['page'] : 0;
        $totalPage = ceil($count / $perPage);
        $pageObj = $this->page_index($count, $totalPage, $page);
        $list = M('event_apply')->join('lr_event on lr_event.id = lr_event_apply.event_id')->field('lr_event_apply.*, lr_event.title')->where($where)->order('apply_status asc, id asc')->limit($page * $perPage, $perPage)->select();

        $this->assign('list', $list);
        $this->assign('pageObj', $pageObj);
        $this->display();
    }

    /**
     * 预约审核通过
     */
    function CulSuc()
    {
        $id = intval($_GET['id']);
        if (empty($id)) {
            $this->error('操作失败.');
        }
        $info =$this->cultural_model->where('id=' . $id)->find();
        if (!$info) {
            $this->error('信息错误.');
        }

        if (intval($info['review']) == 1) {
            $this->success('操作成功！.');
        }

        $result = $this->cultural_model->where('id=' . $id)->save(['review' => 1]);


        if ($result) {
           $this->success("操作成功！");
        } else {
            $this->error('操作失败.');
        }
    }

    /**
     * 预约审核被拒绝
     */
    public function CulFail()
    {
        $id = intval($_GET['id']);
        $data = [
            'review' => 2
        ];

        $result = $this->cultural_model->where(['id' => $id])->save($data);
        if ($result) {
            $this->success("操作成功！");
        } else {
            $this->error('操作失败.');
        }
    }

    /**
     * 返回的base64位
     */
    public function _get_image_name($base64_img,$k)
    {
        $up_dir = './Data/UploadFiles/';
        if(!file_exists($up_dir)){
            mkdir($up_dir,0777);
        }
        if(preg_match('/^(data:\s*image\/(\w+);base64,)/', $base64_img, $result)){
            $type = $result[2];
            if(in_array($type,array('pjpeg','jpeg','jpg','gif','bmp','png'))){
                $new_file = $up_dir.date('YmdHis_')."-".$k.'.'.$type;
                if(file_put_contents($new_file, base64_decode(str_replace($result[1], '', $base64_img)))){
                    $img_path = str_replace('../../..', '', $new_file);
                }
            }
        }
        //返回图片路径
        return $img_path;
    }
}